import {getDict} from "~/utils";
import { getUnit,downloadFileByFileName } from "@/views/authorization-management/apply-unit/api/apply-unit.js";
import {getFiles} from '@/api/operate-construction-contract';

export default {
    data() {
        return {
            loading:true,
            //上次文件相关
            busId:"",
            modelType:"apply-unit",
            url:'/admin/sys-file/upLodeFileEdit',
            //弹出框 初始时隐藏
            dialogVisible: false,
            editObj: {
                applyUnit:{
                    apUiId: '',
                    unitName: '',
                    unitType: '',
                    userName: '',
                    tell: '',
                    creditNumber: "",
                    sort: "",
                    applyProperty: "",
                    latestApplicationTime:"",
                    remarks: ""
                },
                //附件
                fileList: [{
                    name:'',path:''
                }],
                applyUnitLogList:[]
            },
            lists: [],
        }
    },
    mounted:function() {

    },
    created() {
        this.lists = getDict('apply-unit-type');
    },
    computed: {
        applyUnitType: function () {
            let types =getDict('apply-unit-type')
            let pv = this.editObj.applyUnit.unitType;
            if(pv && types.length > 0){
                const pts = types.filter(t => t.value == pv)
                return pts ? pts[0].label : ''
            }
            return ''
        }
    },
    methods: {
        /**
         * 示新增弹框
         */
        show(rowData){


            this.loading = true;

            if(rowData!=null){
                //赋值
                this.editObj.applyUnit.apUiId = rowData.apUiId;
                this.editObj.applyUnit.unitName = rowData.unitName;
                this.editObj.applyUnit.unitType = rowData.unitType;
                this.editObj.applyUnit.userName = rowData.userName;
                this.editObj.applyUnit.tell = rowData.tell;
                this.editObj.applyUnit.creditNumber = rowData.creditNumber;
                this.editObj.applyUnit.sort = rowData.sort;
                this.editObj.applyUnit.applyProperty = rowData.applyProperty;
                this.editObj.applyUnit.latestApplicationTime = rowData.latestApplicationTime;
                this.editObj.applyUnit.remarks = rowData.remarks;

                getUnit(rowData.apUiId).then((response)=>{
                    let applyUnitVo = response.data.data;
                    this.editObj.applyUnitLogList = applyUnitVo.applyUnitLogList;
                });

                if ( this.editObj.applyUnit.apUiId != '') {
                    getFiles(this.editObj.applyUnit.apUiId, this.modelType).then(resp => {
                        if (resp.data.code === 0) {
                            this.editObj.fileList = [...resp.data.data]
                            this.loading = false
                        } else {
                        }
                    }).catch(error => {
                        this.$message({
                            type: 'error',
                            message: '附件获取失败'
                        });
                    }).finally(() => this.loading = false);
                }


            }
            this.dialogVisible = true
        },


        /**
         * 关闭弹窗
         * @param done
         */
        beforeClose(done){
            this.dialogVisible = false
            done();
        },


        /**
         * 附件下载
         * @param path 文件路径
         * @param name 文件名
         */
        downloadFile(path,name){
            downloadFileByFileName(path).then((res)=>{
                // 关闭loading
                this.loading = false
                // 此处有个坑。这里用content保存文件流，最初是content=res，但下载的test.xls里的内容如下图1，
                // 检查了下才发现，后端对文件流做了一层封装，所以将content指向res.data即可
                // 另外，流的转储属于浅拷贝，所以此处的content转储仅仅是便于理解，并没有实际作用=_=
                const content = res.data
                const blob = new Blob([content]) // 构造一个blob对象来处理数据
                const fileName = name // 导出文件名
                // 对于<a>标签，只有 Firefox 和 Chrome（内核） 支持 download 属性
                // IE10以上支持blob但是依然不支持download
                if ('download' in document.createElement('a')) { // 支持a标签download的浏览器
                    const link = document.createElement('a') // 创建a标签
                    link.download = fileName // a标签添加属性
                    link.style.display = 'none'
                    link.href = URL.createObjectURL(blob)
                    document.body.appendChild(link)
                    link.click() // 执行下载
                    URL.revokeObjectURL(link.href) // 释放url
                    document.body.removeChild(link) // 释放标签
                } else { // 其他浏览器
                    navigator.msSaveBlob(blob, fileName)
                }
                this.btnSendTem = false
            }).catch(error => {
                this.$message({
                    type: 'error',
                    message: '操作失败'
                });
            }).finally(() => this.loading = false);
        },






    }


}
